package com.example.service.impl;

import com.example.context.BaseContext;
import com.example.dto.BookDishDTO;
import com.example.dto.ShoppingCartDTO;
import com.example.entity.ShoppingCar;
import com.example.mapper.BookDishMapper;
import com.example.mapper.ShoppingCarMapper;
import com.example.service.ShoppingCarService;
import com.example.vo.BookDishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppCarMapper;

    @Autowired
    private BookDishMapper bookDishMapper;


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void shoppingCar(ShoppingCartDTO shoppingCartDTO) {

        //判断购物车中是否存在商品
        ShoppingCar shoppingCar =new ShoppingCar();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCar);
        Long userId= BaseContext.getCurrentId();
        shoppingCar.setUserId(userId);
        List<ShoppingCar> list = shoppCarMapper.shoppingCar(shoppingCar);

        //存在商品数量加一
        if(list !=null && list.size()>0){
            ShoppingCar cart= list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppCarMapper.updateNumberById(cart);
        }else {
            //不存在，加入商品
            Long setmealId = shoppingCar.getSetmealId();
            BookDishVO dish = bookDishMapper.getById(setmealId);
            shoppingCar.setName(dish.getName());
            shoppingCar.setImage(dish.getImage());
            shoppingCar.setAmount(dish.getPrice());
            shoppingCar.setNumber(1);
            shoppingCar.setCreateTime(LocalDateTime.now());

        }
        shoppCarMapper.insert(shoppingCar);
    }


    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCar> getShoppingCar() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCar shoppingCar = ShoppingCar.builder()
                .userId(userId)
                .build();
        return shoppCarMapper.shoppingCar(shoppingCar);
    }


    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCar() {
        Long userId = BaseContext.getCurrentId();
        shoppCarMapper.cleanShoppingCar(userId);
    }


    /**
     * 删除一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCar shoppingCar = new ShoppingCar();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCar);

        //设置查询条件，查询当前登录用户的购物车数据
        shoppingCar.setUserId(BaseContext.getCurrentId());

        List<ShoppingCar> list = shoppCarMapper.shoppingCar(shoppingCar);

        if(list != null && list.size() > 0){
            shoppingCar = list.get(0);

            Integer number = shoppingCar.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                shoppCarMapper.deleteById(shoppingCar.getId());
            }else {
                //当前商品在购物车中的份数不为1，修改份数即可
                shoppingCar.setNumber(shoppingCar.getNumber() - 1);
                shoppCarMapper.updateNumberById(shoppingCar);
            }
        }
    }


}


